﻿Public Class ucEditApplication4
	Inherits System.Web.UI.UserControl

	Public Event NextStep()
	Public Event BackStep()
	Public Event Cancel()
	Public Event Alert(ByVal msg As String)

	Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		If Not Page.IsPostBack Then
			LoadHeader()
			LoadBank()
			If GlobalApp.DataSourceEditOfficer IsNot Nothing Then
				LoadData()
			End If
		End If
	End Sub

	Private Sub LoadBank()
		Me.BANK_ID.DataTextField = "BANK_NAME"
		Me.BANK_ID.DataValueField = "BANK_ID"
		Me.BANK_ID.DataSource = BL.Bank.GetAll
		Me.BANK_ID.DataBind()
		Me.BANK_ID.Items.Insert(0, New ListItem("--กรุณาระบุชื่อธนาคาร--", "0"))
	End Sub

	Private Sub LoadData()
		Dim registerInfo As Entity.OfficerInfoItem
		registerInfo = GlobalApp.DataSourceEditOfficer
		With registerInfo
			Me.OFFICER_BANK_ACCOUNT_NAME.Text = .officer_bank_account_name
			Common.Utility.SelectChoice(Me.BANK_ID, .bank_id)
			Me.OFFICER_BANK_BRANCH.Text = .officer_bank_branch
			Me.OFFICER_BANK_ACCOUNT_CODE.Text = .officer_bank_account_code
		End With
	End Sub

	Public Sub LoadHeader()
		Me.ucHeaderInfo1.LoadDataEdit()
	End Sub

	Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnNext.Click
		Dim registerInfo As Entity.OfficerInfoItem
		If GlobalApp.DataSourceEditOfficer IsNot Nothing Then
			registerInfo = GlobalApp.DataSourceEditOfficer
		Else
			registerInfo = New Entity.OfficerInfoItem
		End If

		With registerInfo
			.officer_bank_account_name = Me.OFFICER_BANK_ACCOUNT_NAME.Text
			.bank_id = Me.BANK_ID.SelectedValue
			.officer_bank_branch = Me.OFFICER_BANK_BRANCH.Text
			.officer_bank_account_code = Me.OFFICER_BANK_ACCOUNT_CODE.Text
			If Me.BANK_ID.SelectedValue.Equals("0") Then
				.bank_name = String.Empty
			Else
				.bank_name = Me.BANK_ID.SelectedItem.Text
			End If
		End With

		GlobalApp.DataSourceEditOfficer = registerInfo
		RaiseEvent NextStep()
		Me.btnSave.Visible = True
		Me.btnNext.Visible = False

		Me.OFFICER_BANK_ACCOUNT_NAME.Enabled = False
		Me.BANK_ID.Enabled = False
		Me.OFFICER_BANK_BRANCH.Enabled = False
		Me.OFFICER_BANK_ACCOUNT_CODE.Enabled = False
	End Sub

	Protected Sub btnBack_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnBack.Click
		RaiseEvent BackStep()
		Response.Redirect("EditApplication2.aspx", False)
	End Sub

	Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCancel.Click
		RaiseEvent Cancel()
		If HttpContext.Current.User.IsInRole("admin") Then
			Response.Redirect("OfficerManagement.aspx", False)
		Else
			Response.Redirect("ViewStatusRegister.aspx", False)
		End If
	End Sub

	Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click
		Try
			If GlobalApp.DataSourceEditOfficer IsNot Nothing Then
				If BL.Officer.UpdateOfficer(GlobalApp.DataSourceEditOfficer, GlobalApp.DataSourceEditPWD) Then
					Session.Remove(GlobalApp.sessionNameEdit)
					Session.Remove(GlobalApp.sessionNameEditPWD)
					If HttpContext.Current.User.IsInRole("admin") Then
						Response.Redirect("OfficerManagement.aspx", False)
					Else
						Response.Redirect("ViewStatusRegister.aspx", False)
					End If
				Else
					RaiseEvent Alert("ไม่สามารถบันทึกใบสมัครได้")
				End If
			Else
				RaiseEvent Alert("ข้อมูลใบสมัครไม่สมบรูณ์ กรุณาทำการกรอกข้อมูลใบสมัครอีกครั้ง")
			End If
		Catch ex As Exception
			Common.LogError.AddLogException(ex)
			RaiseEvent Alert("ระบบไม่สามารถทำการบันทึกใบสมัครได้ กรุณาติดต่อเจ้าหน้า")
		End Try

	End Sub
End Class